import { routers } from '@/router/router'
import cookie from '@/lib/auth'
const common = {
  namespaced: true, // 开启模块化
  state: {
    version: '1.00', // ..版本号
    loading: false, // loading
    loadingText: '加载中',
    isStopLoading: false, // 当前请求不采用loading
    userInfo: {}, // 用户信息
    isCollapse: false, // 菜单缩放参数
    appRouters: routers, // 本地路由合集
    menuList: [], // 菜单合集
    breadCrumb: [], // 面包屑合集
    menuActive: '', // 菜单高亮参数
    tagsViews: [], // 打开过的路由标签
    iframesUrl: '/index.html#/index-home', // iframes默认路径
    pathname: '/index.html', // 内嵌项目pathname
    indexRouter: 'index-home', // 内嵌项目默认路由
    mianPathname: '/user.html' // 内嵌项目入口
  },
  mutations: {
    /**
     * 设置state
     * @param {*} state
     * @param {*} params  传入OBject 对象
     */
    SET_STATE(state, params) {
      Object.assign(state, params)
    },
    /**
     * 设置面包屑
     * @param {*} state
     * @param {*} params
     */
    SET_B_CRUMB(state, params) {
      const toRouter = {}
      toRouter.title = params.meta.title
      toRouter.name = params.name
      if (params.meta.parent && params.meta.parent.length) {
        if (params.name === 'iframes') {
          state.appRouters.forEach(item => {
            if (item.name === params.meta.auth) {
              toRouter.name = item.name
              toRouter.title = item.meta.title
            } else if (item.children) {
              item.children.forEach(child => {
                if (child.name === params.query.auth) {
                  toRouter.name = child.name
                  toRouter.title = child.title
                  const mapRouter = child.meta.parent.map(item => {
                    item.name = params.name
                    return item
                  })
                  state.breadCrumb = [...mapRouter, toRouter]
                }
              })
            }
          })
          return
        }
        state.breadCrumb = [...params.meta.parent, toRouter]
        return
      }
      state.breadCrumb = [toRouter]
    },

    /**
     * 设置菜单高亮
     * @param {*} state
     * @param {*} params
     */
    SET_MENU_ACTIVE(state, params) {
      console.log(params.path)
      if (params.name === 'iframes') {
        state.menuActive = params.query.auth + ' ' + params.query.pathname
      } else {
        state.menuActive = params.name + ' ' + params.meta.pathname
      }
    },

    /**
     * 设置页面标签
     * @param {*} state
     * @param {*} params
     */
    SET_TAGS_VIEWS(state, params) {
      state.tagsViews = cookie.get('tagsViews') ? JSON.parse(cookie.get('tagsViews')) : []
      const toRouter = {
        // path: location.href,
        name: params.name,
        title: params.meta.title,
        query: params.query,
        fullPath: params.fullPath
      }
      // 匹配路由配置存储到本地
      if (params.name === 'iframes') {
        state.appRouters.forEach(item => {
          if (item.name === params.meta.auth) {
            toRouter.name = item.name
            toRouter.fullPath = '/' + item.name
          } else if (item.children) {
            item.children.forEach(child => {
              if (child.name === params.query.auth) {
                toRouter.name = child.name
                toRouter.title = child.title
                toRouter.fullPath = '/' + child.name
                // toRouter.path = location.origin + child.meta.pathname + '#/' + child.name
              }
            })
          }
        })
      }
      // 忽略已有菜单和配置忽略菜单
      if (state.tagsViews && !params.meta.single) {
        if (!state.tagsViews.some(item => {
            return item.name === toRouter.name || item.path === toRouter.path
          })) {
          state.tagsViews.push(toRouter)
        }
      }
      cookie.set('tagsViews', JSON.stringify(state.tagsViews))
    },
    /**
     * 删除指定的tag标签
     * @param {*} state
     * @param {*} index
     */
    CLOSE_TAG(state, index) {
      state.tagsViews = cookie.get('tagsViews') ? JSON.parse(cookie.get('tagsViews')) : []
      state.tagsViews.splice(index, 1)
      cookie.set('tagsViews', JSON.stringify(state.tagsViews))
    },
    /**
     * 删除所有标签，只留第一个标签
     * @param {*} state
     */
    CLOSE_ALL_TAG(state) {
      state.tagsViews = cookie.get('tagsViews') ? JSON.parse(cookie.get('tagsViews')) : []
      state.tagsViews.splice(1, state.tagsViews.length - 1)
      cookie.set('tagsViews', JSON.stringify(state.tagsViews))
    },
    /**
     * 删除所有标签，只留自身
     * @param {*} state
     */
    CLOSE_OTHER_TAG(state, index) {
      state.tagsViews = cookie.get('tagsViews') ? JSON.parse(cookie.get('tagsViews')) : []
      state.tagsViews = state.tagsViews.filter((item, i) => {
        return i === index
      })
      cookie.set('tagsViews', JSON.stringify(state.tagsViews))
    }

  },
  actions: {
    // 添加标签
    ACTIONS_SET_TAGS({
      state,
      commit
    }, payload) {
      commit('SET_TAGS_VIEWS', payload)
    },
    // 关闭标签
    ACTIONS_DEL_TAGS({
      state,
      commit
    }, payload) {
      commit('CLOSE_TAG', payload)
    },
    // 关闭所有标签
    ACTION_CLOSE_ALL_TAG({
      state,
      commit
    }) {
      commit('CLOSE_ALL_TAG')
    },
    // 关闭除了自身的所有标签
    ACTION_CLOSE_OTHER({
      state,
      commit
    }, payload) {
      commit('CLOSE_OTHER_TAG', payload)
    }
  }
}

export default common
